Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SmartPtr: Use shared ptr to manage GB objects. v6.0.126 #4080

Merged
merged 16 commits into from
Jun 12, 2024

Conversation

winlinvip
Copy link
Member

The object relations:

gb

Session manages SIP and Media object using shared resource or shared ptr. Note that I actually use SrsExecutorCoroutine to delete the object when each coroutine is done, because there is always a dedicate coroutine for each object.

For SIP and Media object, they directly use the session by raw pointer, it's safe because session always live longer than session and media object.

@winlinvip winlinvip added the EnglishNative This issue is conveyed exclusively in English. label Jun 7, 2024
trunk/src/app/srs_app_gb28181.cpp Outdated Show resolved Hide resolved
trunk/src/app/srs_app_conn.hpp Outdated Show resolved Hide resolved
// SrsSharedPtr<MyClass> cp = ptr;
// cp->do_something();
template<class T>
class SrsSharedPtr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems SrsSharedPtr shared similar api with std::shared_ptr, that's good, so why not add a compiling option to control whether use a template alias from std::shared_ptr or self defined shared ptr.

Suggested change
class SrsSharedPtr
#ifdef _USE_STD_SMART_PTR
template <class T>
using SrsSharedPtr = std::shared_ptr<T>;
#else
template <class T>
class SrsSharedPtr {
// current definition
};
#endif

The benefit is to quickly workaround the shared ptr bugs in the future, e.g. the ref_count_ is not atomic, it's ok for current single-thread coroutine solution, but what if multi-thread coroutine are implemented in the future, then it's disaster. For the basic level infrastructure codes, it's a smart idea to always has a replacement solution.

Copy link
Member Author

@winlinvip winlinvip Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't fix. Using std::shared_ptr does not provide any benefits because if SrsSharedPtr has a bug, we would need to fix it.

TRANS_BY_GPT4

@@ -81,4 +81,103 @@ class impl_SrsAutoFree
}
};

// Shared ptr smart pointer, see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a problem of this header file srs_core_autofree.hpp, but the empty srs_core_autofree.cpp, there is no reason to keep a empty cpp file except to waste compiler power.

Copy link
Member Author

@winlinvip winlinvip Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To save on compilation time, please provide the specific amount of time saved by reducing one cpp file.
This is unrelated to the current PR; please submit a separate PR for this matter.

TRANS_BY_GPT4

return err;
}
// Wait for the SIP connection to be terminated.
while (true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This while loop never break except return, is it a problem?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems no problem.


if ((err = resource->start()) != srs_success) {
srs_freep(conn);
SrsExecutorCoroutine* executor = new SrsExecutorCoroutine(_srs_gb_manager, conn, raw_conn, raw_conn);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think executor instance here is a memory leak. Here is the reason:

SrsExecutorCorountine is an ISrsResource which means it can use an ISrsResourceManager to manager its lifecycle, which is _srs_gb_manager in this case, check codes in SrsExecutorCoroutine::cycle():

srs_error_t SrsExecutorCoroutine::cycle()
{
    srs_error_t err = handler_->cycle();
    if (callback_) callback_->on_executor_done(this);
    manager_->remove(this);
    return err;
}

We have manager_->remove(this);, but where is the _srs_gb_manager->add(executor);?

And Check anther two SrsExecutorCoroutine instances, for SrsLazyGbMediaTcpConn and SrsLazyGbSession, they have same problem.

I didn't running any memory leak detector to confirm yet, but I think valgrind can detect it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems no problem. Won't fix.

trunk/src/app/srs_app_conn.hpp Show resolved Hide resolved
@winlinvip winlinvip changed the title SmartPtr: Use shared ptr to manage GB objects. SmartPtr: Use shared ptr to manage GB objects. v5.0.214 v6.0.126 Jun 12, 2024
@winlinvip winlinvip added the RefinedByAI Refined by AI/GPT. label Jun 12, 2024
@winlinvip winlinvip changed the title SmartPtr: Use shared ptr to manage GB objects. v5.0.214 v6.0.126 SmartPtr: Use shared ptr to manage GB objects. v6.0.126 Jun 12, 2024
@winlinvip winlinvip merged commit 6834ec2 into ossrs:develop Jun 12, 2024
4 of 5 checks passed
winlinvip added a commit to winlinvip/srs that referenced this pull request Jun 13, 2024
@winlinvip
Copy link
Member Author

See #3667 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
EnglishNative This issue is conveyed exclusively in English. RefinedByAI Refined by AI/GPT.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants